from django.db import models
from django.contrib.auth.models import User


# Create your models here.
class Customer(models.Model):
    '''客户信息表'''
    qq = models.CharField(max_length=64, unique=True)
    name = models.CharField(max_length=32, blank=True, null=True)
    qq_name = models.CharField(max_length=64, blank=True, null=True)
    phone = models.CharField(max_length=24, blank=True, null=True)
    source_choice = ((0, '介绍'),
                     (1, 'qq'),
                     (2, '官网'),
                     )
    source = models.SmallIntegerField(choices=source_choice)
    referral_from = models.CharField(verbose_name='介绍人', blank=True, null=True, max_length=16)
    consult_course = models.ForeignKey('Course', verbose_name='咨询课程', on_delete=None)
    tags = models.ManyToManyField('Tag')
    status_choice = ((0, '已报名'),
                     (1, '未报名'))
    status = models.SmallIntegerField(choices=status_choice, default=1)
    consultant = models.ForeignKey('UserProfile', on_delete=None)
    memo = models.TextField(blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '客户信息'
        verbose_name_plural = verbose_name


class Course(models.Model):
    '''课程表'''
    name = models.CharField(max_length=64, unique=True)
    price = models.PositiveIntegerField()
    period = models.PositiveSmallIntegerField(verbose_name='周期（月）')
    outline = models.TextField()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '课程列表'
        verbose_name_plural = verbose_name


class Tag(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name


class CustomerFollowUp(models.Model):
    customer = models.ForeignKey(Customer, on_delete=None)
    consultant = models.ForeignKey('UserProfile', on_delete=None)
    content = models.TextField(verbose_name='跟进内容')
    intention_choice = ((0, '2周内'),
                        (1, '1月内'),
                        (2, '近期无'),
                        (3, '已其他'),
                        (4, '已报名'),
                        (5, '已拉黑')
                        )
    intention = models.SmallIntegerField(choices=intention_choice)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{0} : {1}".format(self.customer.qq, self.intention)

    class Meta:
        verbose_name = '客户关系跟进'
        verbose_name_plural = verbose_name


class Branch(models.Model):
    name = models.CharField(max_length=128, unique=True)
    addr = models.CharField(max_length=128)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '校区'
        verbose_name_plural = verbose_name


class ClassList(models.Model):
    branch = models.ForeignKey('Branch', verbose_name='校区', on_delete=None)
    course = models.ForeignKey('Course', verbose_name='课程', on_delete=None)
    class_type_choice = ((0, '全'),
                         (1, '半'),
                         (2, '网络'))
    class_type = models.SmallIntegerField(choices=class_type_choice)
    semester = models.PositiveSmallIntegerField(verbose_name='学期')
    teachers = models.ManyToManyField('UserProfile')
    start_date = models.DateField()
    end_date = models.DateField(blank=True, null=True)

    def __str__(self):
        return self.semester

    class Meta:
        unique_together = ('branch', 'course', 'semester')
        verbose_name = '班级'
        verbose_name_plural = verbose_name


class CourseRecord(models.Model):
    from_class = models.ForeignKey('ClassList', on_delete=None)
    day_num = models.PositiveSmallIntegerField()
    teacher = models.ForeignKey('UserProfile', on_delete=None)
    has_homework = models.BooleanField(default=True)
    homework_title = models.CharField(max_length=128, blank=True, null=True)
    homework_content = models.TextField(blank=True, null=True)
    outline = models.TextField()
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return "%s %s" % (self.from_class, self.day_num)

    class Meta:
        unique_together = ("from_class", "day_num")
        verbose_name_plural = "上课记录"


class StudyRecord(models.Model):
    student = models.ForeignKey('Enrollment', on_delete=None)
    course_record = models.ForeignKey('CourseRecord', on_delete=None)
    attendance_choice = ((0, '签到'),
                         (1, '迟到'),
                         (2, '缺勤'),
                         (3, '早退'),
                         )
    attendance = models.SmallIntegerField(choices=attendance_choice)
    score_choices = ((100, "A+"),
                     (90, "A"),
                     (85, "B+"),
                     (80, "B"),
                     (75, "B-"),
                     (70, "C+"),
                     (60, "C"),
                     (40, "C-"),
                     (-50, "D"),
                     (-100, "COPY"),
                     (0, "N/A"),
                     )
    score = models.SmallIntegerField(choices=score_choices, default=0)
    memo = models.TextField(blank=True, null=True)
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return "{0},{1}".format(self.student, self.score)

    class Meta:
        unique_together = ('student', 'course_record')
        verbose_name = '学习记录'


class Enrollment(models.Model):
    '''报名表'''
    customer = models.ForeignKey(Customer, on_delete=None)
    enrolled_class = models.ForeignKey('ClassList', verbose_name='所报班级', on_delete=None)
    consultant = models.ForeignKey("UserProfile", verbose_name="课程顾问", on_delete=None)
    contract_agreed = models.BooleanField(default=False, verbose_name="学员已同意合同条款")
    contract_approved = models.BooleanField(default=False, verbose_name="合同已审核")
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s %s" % (self.customer, self.enrolled_class)

    class Meta:
        unique_together = ("customer", "enrolled_class")
        verbose_name_plural = "报名表"


class Payment(models.Model):
    customer = models.ForeignKey('Customer', on_delete=None)
    course = models.ForeignKey("Course", verbose_name="所报课程", on_delete=None)
    amount = models.PositiveIntegerField(verbose_name="数额", default=500)
    consultant = models.ForeignKey("UserProfile", on_delete=None)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s %s" % (self.customer, self.amount)

    class Meta:
        verbose_name_plural = "缴费记录"


class UserProfile(models.Model):
    name = models.CharField(max_length=32)
    user = models.OneToOneField(User, on_delete=None)
    roles = models.ManyToManyField('Role')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '账户'
        verbose_name_plural = verbose_name


class Role(models.Model):
    name = models.CharField(max_length=32, unique=True)
    menus = models.ManyToManyField('Menu')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '角色'
        verbose_name_plural = verbose_name


class Menu(models.Model):
    name = models.CharField(max_length=32, unique=True)
    url_name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '菜单'
        verbose_name_plural = verbose_name
